package br.com.trophy.parsing;

import java.util.Vector;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

public class GamesHandler extends DefaultHandler
{
	// ==========================================================================
	// Atributos de classe
	// ==========================================================================

	private enum TAGS
	{
		games, 
		game, 
		id, 
		perc_done, 
		pic, 
		pic_big, 
		name, 
		trophy_count, 
		last_trophy, 
		platinum, 
		silver, 
		bronze, 
		gold, 
		type, 
		date
	}
	
	private StringBuffer tempString;

	private Game tempJogo;
	private Vector<Game> jogos;
	private boolean isGame;
	private boolean isId;
	private boolean isPercDone;
	private boolean isPic;
	private boolean isPicBig;
	private boolean isName;
	private boolean isTrophyCount;
	private boolean isLastTrophy;
	private boolean isPlatinum;
	private boolean isGold;
	private boolean isSilver;
	private boolean isBronze;
	private boolean isType;
	private boolean isDate;

	// ==========================================================================
	// Construtor
	// ==========================================================================
	public GamesHandler()
	{
		super();
		this.jogos = new Vector<Game>();
		this.tempString = new StringBuffer();
	}
	// ==========================================================================
	// Metodos Publicos
	// ==========================================================================
	public Vector<Game> getParsedJogos()
	{
		return jogos;
	}
	// ==========================================================================
	@Override
	public void startDocument()
	{
	}
	// ==========================================================================
	@Override
	public void endDocument()
	{
	}
	// ==========================================================================
	@Override
	public void characters( char ch[], int start, int length )
	{
		if ( isGame   || isId     || isPercDone    || isPic        || 
			 isPicBig || isName   || isTrophyCount || isLastTrophy || 
			 isType   || isDate   || isPlatinum    || isGold 	   || 
			 isSilver || isBronze  )
		{
			tempString.append( ch, start, length );
		}
	}
	// ==========================================================================
	@Override
	public void startElement( String n, String l, String q, Attributes a )
	{
		switch ( TAGS.valueOf( l ) )
		{
		case game:
			tempJogo = new Game();
			break;
		case id:
			isId = true;
			tempString.setLength( 0 );
			break;
		case perc_done:
			isPercDone = true;
			tempString.setLength( 0 );
			break;
		case pic:
			isPic = true;
			tempString.setLength( 0 );
			break;
		case pic_big:
			isPicBig = true;
			tempString.setLength( 0 );
			break;
		case name:
			isName = true;
			tempString.setLength( 0 );
			break;
		case trophy_count:
			isTrophyCount = true;
			tempString.setLength( 0 );
			break;
		case last_trophy:
			isLastTrophy = true;
			tempString.setLength( 0 );
			break;
		case platinum:
			isPlatinum = true;
			tempString.setLength( 0 );
			break;
		case gold:
			isGold = true;
			tempString.setLength( 0 );
			break;
		case silver:
			isSilver = true;
			tempString.setLength( 0 );
			break;
		case bronze:
			isBronze = true;
			tempString.setLength( 0 );
			break;
		case type:
			isType = true;
			tempString.setLength( 0 );
			break;
		case date:
			isDate = true;
			tempString.setLength( 0 );
			break;
		default:
			tempString.setLength( 0 );
			break;
		}
	}
	// ==========================================================================
	@Override
	public void endElement( String n, String l, String q )
	{
		switch ( TAGS.valueOf( l ) )
		{
		case id:
			isId = false;
			tempJogo.setId( tempString.toString() );
			break;
		case perc_done:
			isPercDone = false;
			tempJogo.setPercDone( tempString.toString() );
			break;
		case pic:
			String s = tempJogo.getPic();
			if( s == null)
			{
				isPic = false;
				tempJogo.setPic( tempString.toString() );
				break;
			}
			else
			{
				break;
			}
		case pic_big:
			String str = tempJogo.getBigPic();
			if( str == null )
			{
				isPicBig = false;
				tempJogo.setBigPic( tempString.toString() );
				break;
			}
			else
			{
				break;
			}
		case name:
			if(tempString.toString() != "")
			{
				isName = false;
				tempJogo.setName( tempString.toString() );
				break;
			}
			else
				break;
		case trophy_count:
			isTrophyCount = false;
			tempJogo.setCountTrophies( tempString.toString() );
			break;
		case platinum:
			isPlatinum = false;
			tempJogo.setCountPlatinum( tempString.toString() );
			break;
		case gold:
			isGold = false;
			tempJogo.setCountGold( tempString.toString() );
			break;
		case silver:
			isSilver = false;
			tempJogo.setCountSilver( tempString.toString() );
			break;
		case bronze:
			isBronze = false;
			tempJogo.setCountBronze( tempString.toString() );
			break;
		case type:
			isType = true;
			tempJogo.setType( tempString.toString() );
			break;
		case date:
			isDate = true;
			tempJogo.setDate( tempString.toString() );
			break;
		case game:
			jogos.add( tempJogo );
			break;
		default:
			break;
		}
	}
}// end class Gameshandler
